<template>
  <div class="classroom-home">
    <!-- 欢迎横幅 -->
    <div class="welcome-banner">
      <div class="banner-content">
        <div class="welcome-text">
          <h1>欢迎来到智能课堂</h1>
          <p>探索知识的海洋，开启学习新篇章</p>
        </div>
        <div class="banner-stats">
          <div class="stat-item">
            <div class="stat-number">{{ stats.totalCourses }}</div>
            <div class="stat-label">精品课程</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ stats.totalStudents }}</div>
            <div class="stat-label">学习人数</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ stats.totalHours }}</div>
            <div class="stat-label">学习时长</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 快速导航 -->
    <div class="quick-nav">
      <div class="nav-content">
        <div 
          v-for="nav in quickNavs" 
          :key="nav.id"
          class="nav-item"
          @click="handleNavClick(nav)"
        >
          <div class="nav-icon">
            <el-icon><component :is="nav.icon" /></el-icon>
          </div>
          <div class="nav-info">
            <div class="nav-title">{{ nav.title }}</div>
            <div class="nav-desc">{{ nav.description }}</div>
          </div>
        </div>
      </div>
    </div>

    <!-- 推荐课程 -->
    <div class="featured-courses">
      <div class="section-header">
        <h2>推荐课程</h2>
        <el-button text @click="viewAllCourses">
          查看全部 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      
      <div class="courses-grid">
        <div 
          v-for="course in featuredCourses" 
          :key="course.id"
          class="course-card"
          @click="viewCourse(course)"
        >
          <div class="course-image">
            <img :src="course.cover" :alt="course.title" />
            <div class="course-overlay">
              <el-button circle type="primary">
                <el-icon><VideoPlay /></el-icon>
              </el-button>
            </div>
          </div>
          
          <div class="course-content">
            <div class="course-category">{{ course.category }}</div>
            <h3 class="course-title">{{ course.title }}</h3>
            <p class="course-desc">{{ course.description }}</p>
            
            <div class="course-meta">
              <div class="instructor">
                <el-avatar :size="24" :src="course.instructor.avatar">
                  {{ course.instructor.name.charAt(0) }}
                </el-avatar>
                <span>{{ course.instructor.name }}</span>
              </div>
              <div class="course-stats">
                <span class="students">{{ formatNumber(course.students) }}人学习</span>
                <span class="rating">
                  <el-icon><Star /></el-icon>
                  {{ course.rating }}
                </span>
              </div>
            </div>
            
            <div class="course-progress" v-if="course.progress !== undefined">
              <el-progress 
                :percentage="course.progress" 
                :show-text="false"
                :stroke-width="4"
              />
              <span class="progress-text">{{ course.progress }}%</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 学习路径 -->
    <div class="learning-paths">
      <div class="section-header">
        <h2>学习路径</h2>
        <el-button text @click="viewAllPaths">
          查看全部 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      
      <div class="paths-list">
        <div 
          v-for="path in learningPaths" 
          :key="path.id"
          class="path-card"
          @click="viewPath(path)"
        >
          <div class="path-icon">
            <el-icon><component :is="path.icon" /></el-icon>
          </div>
          
          <div class="path-content">
            <h3>{{ path.title }}</h3>
            <p>{{ path.description }}</p>
            
            <div class="path-meta">
              <span class="duration">{{ path.duration }}</span>
              <span class="courses">{{ path.courses }}门课程</span>
              <span class="level">{{ path.level }}</span>
            </div>
            
            <div class="path-progress">
              <el-progress 
                :percentage="path.progress" 
                :show-text="false"
                :stroke-width="6"
              />
              <span class="progress-text">{{ path.progress }}% 完成</span>
            </div>
          </div>
          
          <div class="path-action">
            <el-button type="primary">
              {{ path.progress > 0 ? '继续学习' : '开始学习' }}
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 最新动态 -->
    <div class="recent-activities">
      <div class="section-header">
        <h2>最新动态</h2>
        <el-button text @click="viewAllActivities">
          查看全部 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      
      <div class="activities-list">
        <div 
          v-for="activity in recentActivities" 
          :key="activity.id"
          class="activity-item"
        >
          <div class="activity-icon">
            <el-icon><component :is="activity.icon" /></el-icon>
          </div>
          
          <div class="activity-content">
            <div class="activity-title">{{ activity.title }}</div>
            <div class="activity-desc">{{ activity.description }}</div>
            <div class="activity-time">{{ formatTime(activity.time) }}</div>
          </div>
          
          <div class="activity-action">
            <el-button size="small" @click="handleActivityAction(activity)">
              {{ activity.actionText }}
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
  ArrowRight, VideoPlay, Star, 
  Monitor, Document, Trophy, TrendCharts,
  CircleCheck, Bell, Gift
} from '@element-plus/icons-vue'

const router = useRouter()

// 响应式数据
const stats = ref({
  totalCourses: '1,200+',
  totalStudents: '50万+',
  totalHours: '100万+'
})

const quickNavs = ref([
  {
    id: 1,
    title: '我的课程',
    description: '查看已购买的课程',
    icon: 'Monitor',
    path: '/classroom/my-courses'
  },
  {
    id: 2,
    title: '学习计划',
    description: '制定个性化学习计划',
    icon: 'Document',
    path: '/classroom/study-plan'
  },
  {
    id: 3,
    title: '学习成就',
    description: '查看学习成就和证书',
    icon: 'Trophy',
    path: '/classroom/achievements'
  },
  {
    id: 4,
    title: '学习统计',
    description: '分析学习数据和进度',
    icon: 'TrendCharts',
    path: '/classroom/statistics'
  }
])

const featuredCourses = ref([
  {
    id: 1,
    title: 'Vue3 + TypeScript 实战开发',
    description: '从零开始学习Vue3和TypeScript，构建现代化前端应用',
    cover: '/images/course-vue3.jpg',
    category: '前端开发',
    instructor: {
      name: '张老师',
      avatar: '/images/instructor-1.jpg'
    },
    students: 12500,
    rating: 4.8,
    progress: 65
  },
  {
    id: 2,
    title: 'React 18 新特性详解',
    description: '深入理解React 18的并发特性和Hooks最佳实践',
    cover: '/images/course-react.jpg',
    category: '前端开发',
    instructor: {
      name: '李老师',
      avatar: '/images/instructor-2.jpg'
    },
    students: 8900,
    rating: 4.7,
    progress: 30
  },
  {
    id: 3,
    title: 'Node.js 后端开发实战',
    description: '使用Node.js构建高性能的后端API服务',
    cover: '/images/course-nodejs.jpg',
    category: '后端开发',
    instructor: {
      name: '王老师',
      avatar: '/images/instructor-3.jpg'
    },
    students: 6700,
    rating: 4.6
  },
  {
    id: 4,
    title: 'Python 数据分析入门',
    description: '学习使用Python进行数据分析和可视化',
    cover: '/images/course-python.jpg',
    category: '数据科学',
    instructor: {
      name: '赵老师',
      avatar: '/images/instructor-4.jpg'
    },
    students: 15200,
    rating: 4.9
  }
])

const learningPaths = ref([
  {
    id: 1,
    title: '前端工程师成长路径',
    description: '从HTML/CSS基础到现代前端框架的完整学习路径',
    icon: 'Monitor',
    duration: '6个月',
    courses: 12,
    level: '初级到高级',
    progress: 45
  },
  {
    id: 2,
    title: '全栈开发者路径',
    description: '掌握前端、后端、数据库的全栈开发技能',
    icon: 'TrendCharts',
    duration: '8个月',
    courses: 18,
    level: '中级到高级',
    progress: 20
  },
  {
    id: 3,
    title: '数据科学入门路径',
    description: '学习数据分析、机器学习和人工智能基础',
    icon: 'Document',
    duration: '4个月',
    courses: 8,
    level: '初级到中级',
    progress: 0
  }
])

const recentActivities = ref([
  {
    id: 1,
    title: '完成了课程学习',
    description: '《Vue3组件开发》- 第3章：组件通信',
    time: new Date(Date.now() - 2 * 60 * 60 * 1000),
    icon: 'CircleCheck',
    actionText: '继续学习'
  },
  {
    id: 2,
    title: '获得新成就',
    description: '连续学习7天，获得"坚持学习"徽章',
    time: new Date(Date.now() - 5 * 60 * 60 * 1000),
    icon: 'Trophy',
    actionText: '查看成就'
  },
  {
    id: 3,
    title: '课程更新提醒',
    description: '《React 18新特性》新增了2个章节',
    time: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000),
    icon: 'Bell',
    actionText: '立即查看'
  },
  {
    id: 4,
    title: '限时优惠',
    description: '前端开发课程包限时8折优惠',
    time: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000),
    icon: 'Gift',
    actionText: '查看详情'
  }
])

// 方法
const formatNumber = (num) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + '万'
  }
  if (num >= 1000) {
    return (num / 1000).toFixed(1) + 'k'
  }
  return num.toString()
}

const formatTime = (time) => {
  const now = new Date()
  const diff = now - time
  const hours = Math.floor(diff / (1000 * 60 * 60))
  const days = Math.floor(hours / 24)
  
  if (days > 0) {
    return `${days}天前`
  } else if (hours > 0) {
    return `${hours}小时前`
  } else {
    return '刚刚'
  }
}

const handleNavClick = (nav) => {
  router.push(nav.path)
}

const viewCourse = (course) => {
  router.push(`/classroom/course/${course.id}`)
}

const viewPath = (path) => {
  router.push(`/classroom/path/${path.id}`)
}

const viewAllCourses = () => {
  router.push('/classroom/courses')
}

const viewAllPaths = () => {
  router.push('/classroom/paths')
}

const viewAllActivities = () => {
  router.push('/classroom/activities')
}

const handleActivityAction = (activity) => {
  // 根据活动类型执行不同操作
  console.log('Activity action:', activity)
}

onMounted(() => {
  // 初始化数据
})
</script>

<style lang="scss" scoped>
.classroom-home {
  min-height: 100vh;
  background: var(--el-bg-color-page);
}

.welcome-banner {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 60px 0;
}

.banner-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  @media (max-width: 768px) {
    flex-direction: column;
    gap: 30px;
    text-align: center;
  }
}

.welcome-text {
  h1 {
    font-size: 3rem;
    font-weight: 700;
    margin-bottom: 16px;
  }
  
  p {
    font-size: 1.25rem;
    opacity: 0.9;
  }
}

.banner-stats {
  display: flex;
  gap: 40px;
  
  @media (max-width: 768px) {
    gap: 20px;
  }
}

.stat-item {
  text-align: center;
  
  .stat-number {
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 4px;
  }
  
  .stat-label {
    font-size: 0.9rem;
    opacity: 0.8;
  }
}

.quick-nav {
  max-width: 1200px;
  margin: -30px auto 0;
  padding: 0 20px;
  position: relative;
  z-index: 10;
}

.nav-content {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.nav-item {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
  }
  
  .nav-icon {
    width: 48px;
    height: 48px;
    background: var(--el-color-primary-light-9);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 16px;
    
    .el-icon {
      font-size: 24px;
      color: var(--el-color-primary);
    }
  }
  
  .nav-title {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--el-text-color-primary);
  }
  
  .nav-desc {
    font-size: 14px;
    color: var(--el-text-color-secondary);
  }
}

.featured-courses,
.learning-paths,
.recent-activities {
  max-width: 1200px;
  margin: 60px auto 0;
  padding: 0 20px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  
  h2 {
    font-size: 24px;
    font-weight: 600;
    color: var(--el-text-color-primary);
  }
}

.courses-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
}

.course-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  }
}

.course-image {
  position: relative;
  height: 180px;
  overflow: hidden;
  
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  
  .course-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s ease;
  }
  
  &:hover .course-overlay {
    opacity: 1;
  }
}

.course-content {
  padding: 20px;
}

.course-category {
  font-size: 12px;
  color: var(--el-color-primary);
  background: var(--el-color-primary-light-9);
  padding: 4px 8px;
  border-radius: 4px;
  display: inline-block;
  margin-bottom: 12px;
}

.course-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--el-text-color-primary);
  @include text-ellipsis(2);
}

.course-desc {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin-bottom: 16px;
  @include text-ellipsis(2);
}

.course-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.instructor {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--el-text-color-regular);
}

.course-stats {
  display: flex;
  gap: 12px;
  font-size: 12px;
  color: var(--el-text-color-placeholder);
  
  .rating {
    display: flex;
    align-items: center;
    gap: 2px;
    
    .el-icon {
      color: #ffd700;
    }
  }
}

.course-progress {
  display: flex;
  align-items: center;
  gap: 8px;
  
  .el-progress {
    flex: 1;
  }
  
  .progress-text {
    font-size: 12px;
    color: var(--el-color-primary);
    font-weight: 500;
  }
}

.paths-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.path-card {
  background: white;
  padding: 24px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  display: flex;
  align-items: center;
  gap: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  }
  
  @media (max-width: 768px) {
    flex-direction: column;
    text-align: center;
  }
}

.path-icon {
  width: 60px;
  height: 60px;
  background: var(--el-color-primary-light-9);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  
  .el-icon {
    font-size: 28px;
    color: var(--el-color-primary);
  }
}

.path-content {
  flex: 1;
  
  h3 {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 8px;
    color: var(--el-text-color-primary);
  }
  
  p {
    font-size: 14px;
    color: var(--el-text-color-secondary);
    margin-bottom: 12px;
  }
  
  .path-meta {
    display: flex;
    gap: 16px;
    margin-bottom: 12px;
    font-size: 12px;
    color: var(--el-text-color-placeholder);
    
    @media (max-width: 768px) {
      justify-content: center;
    }
  }
  
  .path-progress {
    display: flex;
    align-items: center;
    gap: 8px;
    
    .el-progress {
      flex: 1;
      max-width: 200px;
    }
    
    .progress-text {
      font-size: 12px;
      color: var(--el-color-primary);
      font-weight: 500;
    }
  }
}

.path-action {
  flex-shrink: 0;
}

.activities-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.activity-item {
  background: white;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  display: flex;
  align-items: center;
  gap: 16px;
}

.activity-icon {
  width: 40px;
  height: 40px;
  background: var(--el-color-primary-light-9);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  
  .el-icon {
    font-size: 20px;
    color: var(--el-color-primary);
  }
}

.activity-content {
  flex: 1;
  
  .activity-title {
    font-weight: 500;
    margin-bottom: 4px;
    color: var(--el-text-color-primary);
  }
  
  .activity-desc {
    font-size: 14px;
    color: var(--el-text-color-secondary);
    margin-bottom: 4px;
  }
  
  .activity-time {
    font-size: 12px;
    color: var(--el-text-color-placeholder);
  }
}

.activity-action {
  flex-shrink: 0;
}
</style>
